package com.wtwd.wtwddevice.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.constant.DeviceLoginEnum;
import com.wtwd.wtwddevice.mapper.DeviceStatusMapper;
import com.wtwd.wtwddevice.model.entity.DeviceStatus;
import com.wtwd.wtwddevice.service.IDeviceStatusService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-04-28
 */
@Service
public class DeviceStatusServiceImpl extends ServiceImpl<DeviceStatusMapper, DeviceStatus> implements IDeviceStatusService {

    @Resource
    private DeviceStatusMapper deviceStatusMapper;

    @Override
    public boolean insertOrUpdate(DeviceStatus deviceStatus) {
        if (deviceStatus == null || deviceStatus.getDeviceId() == null) {
            return false;
        }
        return deviceStatusMapper.insertOrUpdate(deviceStatus)>0?true:false;
    }

    @Override
    public boolean downLine(Long deviceKey) {
        return deviceStatusMapper.downLine(deviceKey, DeviceLoginEnum.DEVICE_OFFLINE,new Date().getTime())>0?true:false;
    }

    @Override
    public int editDeviceStatus(DeviceStatus deviceStatus) {
        return deviceStatusMapper.editDeviceStatus(deviceStatus);
    }


    @Override
    public boolean updateStatus(Long deviceId, String battery, String step, String wearingState) {
        return deviceStatusMapper.updateStatus(deviceId,battery,step,wearingState,new Date().getTime())>0?true:false;
    }

    @Override
    public boolean getDeviceOnlineState(Long deviceId) {
        if (deviceId==null){
            return false;
        }
        Integer deviceOnlineState = deviceStatusMapper.getDeviceOnlineState(deviceId);
        if (deviceOnlineState==null){
            return false;
        }
        return deviceStatusMapper.getDeviceOnlineState(deviceId)>0?true:false;
    }
}
